package cc.vv.mvp.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cc.vv.baselibrary.fragment.UtimingBaseFragment;
import cc.vv.mvp.binder.BaseDataBinder;
import cc.vv.mvp.delegate.XDelegate;

/**
 * Created by sun.li on 2018/6/6.
 *
 * @author sun.li
 */

public abstract class BaseFragmentMVP<T extends XDelegate> extends UtimingBaseFragment {

    /**
     * 视图托管类对象
     */
    protected T viewDelegate;

    /**
     * 公共数据视图绑定控制器对象
     */
    protected BaseDataBinder binder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            viewDelegate = getDelegateClass().newInstance();
        } catch (java.lang.InstantiationException e) {
            throw new RuntimeException("create IDelegate error");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create IDelegate error");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == viewDelegate.getRootView()) {
            viewDelegate.create(inflater, container, savedInstanceState);
        }
        try {
            if (null != viewDelegate.getRootView()) {
                ViewGroup parent = (ViewGroup) viewDelegate.getRootView().getParent();
                if (null != parent) {
                    parent.removeView(viewDelegate.getRootView());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return viewDelegate.getRootView();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        initData();
        binder = getDataBinder();
        super.onActivityCreated(savedInstanceState);
        viewDelegate.initWidget();
        bindEvenListener();
    }

    @Override
    protected void initView(Bundle bundle) {
        if (null != viewDelegate && null != viewDelegate.getRootView()) {
            try {
                ViewGroup.LayoutParams layoutParams = viewDelegate.getRootView().getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                viewDelegate.getRootView().setLayoutParams(layoutParams);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 初始化界面数据
     */
    protected abstract void initData();

    /**
     * 注册界面控件点击事件
     */
    protected abstract void bindEvenListener();

    /**
     * 指定界面的视图控制器，必须指定
     */
    protected abstract Class<T> getDelegateClass();

    /**
     * 公共数据视图绑定控制器数据对象
     */
    public abstract BaseDataBinder getDataBinder();

    public <D extends Object> void notifyModelChanged(D data) {
        if (binder != null)
            binder.viewBindModel(viewDelegate, data);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (null == viewDelegate) {
            try {
                viewDelegate = getDelegateClass().newInstance();
            } catch (java.lang.InstantiationException e) {
                throw new RuntimeException("create IDelegate error");
            } catch (IllegalAccessException e) {
                throw new RuntimeException("create IDelegate error");
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        /* 界面关闭解除视图绑定操作*/
        viewDelegate = null;
    }
}
